package com.sepro.tim.database.DAO;

import java.util.Vector;

import com.sepro.tim.database.DTO.DTO_Setting;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DAO_Setting extends SQLiteOpenHelper{

	private SQLiteDatabase gDatabase;
	
//	private static final String gLanguage= "Language";
//	
//	private static final String gWaringBefore = "5"; // thoi gian canh bao truoc khi toi cong viec nay
//	
//	private static final String gTimeWaring = "1";	//so lan canh bao cho mot cong viec
//	
//	private static final String gTypeWaring = "TypeWaring";	//loai canh bao cong viec
	
	private static final String TABLE_NAME = "SETTING";
	
	private static final String DATABASE_CREATE = "CREATE TABLE [" + TABLE_NAME + "] (" +
			"[Language] VARCHAR(20)  NULL," +
			"[WaringBefore] INTEGER  NULL," +
			"[TimeWaring] INTEGER  NULL," +
			"[TypeWaring] INTEGER  NULL)";
	
	public DAO_Setting(Context context, String DATABASE_NAME, CursorFactory factory,
			int DATABASE_VERSION) {
		super(context, DATABASE_NAME, factory, DATABASE_VERSION);
		
		gDatabase = context.openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, factory);
		
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(DATABASE_CREATE);	
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(DAO_Setting.class.getName(), "Upgrading database from version " + oldVersion + "to" 
				+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
		onCreate(db);	
	}
	
	public void openWriteDatabase() throws SQLException{
		gDatabase = getWritableDatabase();
	}
	
	public void openReadDatabase() throws SQLException{
		gDatabase = getReadableDatabase();
	}
	
	public void closeDatabase() {
		gDatabase.close();
	}
	
	public void addSetting(DTO_Setting mSetting){
		
		this.openWriteDatabase();
		
		String sql = "INSERT INTO " + TABLE_NAME + "(Language,WaringBefore,TimeWaring,TypeWaring) VALUES(";
		sql += "'" + mSetting.getgLanguage().trim() + "',";
		sql += mSetting.getgWaringBefore() + ",";
		sql += mSetting.getgTimeWaring() + ",";
		sql += mSetting.getgTypeWaring() + ")";
		
		gDatabase.execSQL(sql);
		
//		ContentValues ctValues = new ContentValues();
//		
//		ctValues.put(gLanguage, mSetting.getgLanguage());
//		
//		ctValues.put(gWaringBefore, mSetting.getgWaringBefore().toString());
//		
//		ctValues.put(gTimeWaring, mSetting.getgTimeWaring().toString());
//		
//		ctValues.put(gTypeWaring, mSetting.getgTypeWaring());
//		
//		gDatabase.insert(TABLE_NAME, null, ctValues);
		
//		//ctValues.clear();
		
		this.closeDatabase();
	}
	
	public void updateSetting(DTO_Setting mSetting){
		
		this.openWriteDatabase();
		
		String sql = "UPDATE " + TABLE_NAME + " SET ";
		sql += "Language = '" + mSetting.getgLanguage().trim() + "',";
		sql += "WaringBefore = " + mSetting.getgWaringBefore() + ",";
		sql += "TimeWaring = " + mSetting.getgTimeWaring() + ",";
		sql += "TypeWaring = " + mSetting.getgTypeWaring();
		
		gDatabase.execSQL(sql);
		
//		ContentValues ctValues = new ContentValues();
//		
//		ctValues.put(gLanguage, mSetting.getgLanguage());
//		
//		ctValues.put(gWaringBefore, mSetting.getgWaringBefore().toString());
//		
//		ctValues.put(gTimeWaring, mSetting.getgTimeWaring().toString());
//		
//		ctValues.put(gTypeWaring, mSetting.getgTypeWaring());
//		
//		gDatabase.update(TABLE_NAME, ctValues, null, null);
//		
//		//ctValues.clear();
		
		this.closeDatabase();
	}
	
	public void deleteSetting(String mLanguage){
		
		this.openWriteDatabase();
		
		String sql = "DELETE FROM " + TABLE_NAME + " ";
		sql += "WHERE Language = '" + mLanguage.trim() + "'";
		
		gDatabase.execSQL(sql);
		
		this.closeDatabase();
	}
	
	public DTO_Setting getSetting(String mLanguage)
	{
		this.openReadDatabase();
		
		String sql = "SELECT * FROM " + TABLE_NAME + " " +
				"WHERE Language = '" + mLanguage.trim() + "'";
		
		Cursor mCursor = gDatabase.rawQuery(sql, null);
		
		DTO_Setting mSetting = new DTO_Setting();
		
		mSetting.setgLanguage(mCursor.getString(0).trim());
		mSetting.setgWaringBefore(mCursor.getInt(1));
		mSetting.setgTimeWaring(mCursor.getInt(2));
		mSetting.setgTypeWaring(mCursor.getInt(3));
		
		gDatabase.execSQL(sql);
		
		this.closeDatabase();
		
		return mSetting;
	}
	
	public Vector<DTO_Setting> getAllSetting() {
		
		this.openReadDatabase();
		
		Vector<DTO_Setting> list = new Vector<DTO_Setting>();
		
		DTO_Setting mSetting = new DTO_Setting();
		
		String sql = "SELECT * FROM " + TABLE_NAME;
		
		Cursor mCursor = gDatabase.rawQuery(sql, null);
		
		if(mCursor.moveToFirst())
		{
			do
			{
				mSetting.setgLanguage(mCursor.getString(0).trim());
				mSetting.setgWaringBefore(mCursor.getInt(1));
				mSetting.setgTimeWaring(mCursor.getInt(2));
				mSetting.setgTypeWaring(mCursor.getInt(3));
				
				list.add(mSetting);
				
			}while(mCursor.moveToNext());
			
			this.closeDatabase();
			
			return list;
			
		}
		else {
			
			this.closeDatabase();
			
			return null;
		}
	}
}
